package cn.iocoder.yudao.module.yyzx.service.food;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.yyzx.controller.admin.food.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.food.FoodDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.yyzx.dal.mysql.food.FoodMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 膳食信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class FoodServiceImpl implements FoodService {

    @Resource
    private FoodMapper foodMapper;

    @Override
    public Integer createFood(FoodSaveReqVO createReqVO) {
        // 插入
        FoodDO food = BeanUtils.toBean(createReqVO, FoodDO.class);
        foodMapper.insert(food);
        // 返回
        return food.getId();
    }

    @Override
    public void updateFood(FoodSaveReqVO updateReqVO) {
        // 校验存在
        validateFoodExists(updateReqVO.getId());
        // 更新
        FoodDO updateObj = BeanUtils.toBean(updateReqVO, FoodDO.class);
        foodMapper.updateById(updateObj);
    }

    @Override
    public void deleteFood(Integer id) {
        // 校验存在
        validateFoodExists(id);
        // 删除
        foodMapper.deleteById(id);
    }

    private void validateFoodExists(Integer id) {
        if (foodMapper.selectById(id) == null) {
            throw exception(FOOD_NOT_EXISTS);
        }
    }

    @Override
    public FoodDO getFood(Integer id) {
        return foodMapper.selectById(id);
    }

    @Override
    public PageResult<FoodDO> getFoodPage(FoodPageReqVO pageReqVO) {
        return foodMapper.selectPage(pageReqVO);
    }

}